package com.xzg.apply.controller;

import com.xzg.apply.entity.Apply;
import com.xzg.apply.service.ApplyService;
import com.xzg.common.constants.Constant;
import com.xzg.common.exception.XZGException;
import com.xzg.common.page.PageQueryBean;
import com.xzg.common.resp.APIResult;
import com.xzg.common.vo.QueryCondition;
import com.xzg.mission.entity.Mission;
import com.xzg.user.entity.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/apply")
public class ApplyController {

    @Autowired
    private ApplyService applyService;

    /**
     *@Author Ye
     *Date 2018-03-08 17:16
     *@Description 申请工作流启动
     */
    @RequestMapping("/start")
    @ResponseBody
    public APIResult startApply(@RequestBody Mission mission) throws XZGException {
        APIResult resp = new APIResult<>();
        try{
            applyService.startApply(mission);
            //申请成功
            resp.setMessage("application approved");
        }catch (XZGException e) {
            resp.setMessage(e.getMessage());
            resp.setCode(e.getStatusCode());
        }
        return resp;

    }

    /**
     *@Author Ye
     *Date 2018-03-08 17:16
     *@Description 上级查看所有申请数据
     */
    @RequestMapping("/list")
//    @RequiresPermissions("complete:list")
    @ResponseBody
    public PageQueryBean listTasks(@RequestBody QueryCondition condition) {
       User user = (User) SecurityUtils.getSubject().getSession().getAttribute("userinfo");
//        User user = new User();
//        user.setId(3l);
//        user.setUsername("xiaoye");
        String[] range = condition.getRangeDate().split("/");
        condition.setStartDate(range[0]);
        condition.setEndDate(range[1]);
        PageQueryBean result = applyService.listTask(user.getUsername(),condition);
        return result;
    }

    /**
     *@Author Ye
     *Date 2018-03-08 17:17
     *@Description 上级审批申请
     */
//    @RequiresPermissions("complete:list")
    @RequestMapping("/approve")
    @ResponseBody
    public APIResult approve(@RequestBody QueryCondition condition) {
        Mission mission = new Mission();
        mission.setMissionId(condition.getMissionId());
        Apply apply = new Apply();
        apply.setId(condition.getId());
        apply.setUserId(condition.getUserId());
        apply.setTaskId(condition.getTaskId());
        apply.setStatus(condition.getStatus());
        User user = new User();
        user.setCID(condition.getCID());// 標記
        int i = applyService.approve(apply,mission,user);
        APIResult resp = new APIResult<>();
        if(i == Constant.APPLY_STATUS_PSSS) {
            resp.setMessage("通过");
        }
         else if(i == Constant.APPLY_STATUS_REFUSE) {
            resp.setMessage("拒绝");
        } else {
            resp.setMessage("请求异常");
        }
        return resp;
    }
}
